package com.jifen.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

	/**
	 * 获取系统日期
	 * 
	 * @return
	 */
	public static Date getDate() {
		return new Date();
	}

	/**
	 * 格式化系统日期
	 *
	 * @return
	 */
	public static String formatDate() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	/**
	 * 格式化系统日期
	 * 
	 * @return
	 */
	public static String formatDate(String fmt) {
		return new SimpleDateFormat(fmt).format(new Date());
	}

	/**
	 * 格式化日期
	 * 
	 * @return
	 */
	public static String formatDate(Date date, String fmt) {
		return new SimpleDateFormat(fmt).format(date);
	}

	/**
	 * 字符串日期格式化
	 * @param strDate
	 * @param fmt
	 * @return
	 * @throws ParseException
	 */
	public static String parse(String strDate, String fmt) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(fmt);
			return new SimpleDateFormat(fmt).format(sdf.parse(strDate));
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当月每一天
	 * @return
	 */
	public static List getDayListOfMonth(String date) {
		DateTime dates = DateTime.parse(date, DateTimeFormat.forPattern("yyyy-MM-dd"));
		List list = new ArrayList();
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		aCalendar.set(Calendar.YEAR, dates.getYear());
		aCalendar.set(Calendar.MONTH, dates.getMonthOfYear());
		int year = aCalendar.get(Calendar.YEAR);//年份
		int month = aCalendar.get(Calendar.MONTH);//月份
		//int day = aCalendar.getActualMaximum(Calendar.DATE);
		int day = dates.getDayOfMonth();
		String months = String.valueOf(month);
		if(month<10){
			months = "0"+month;
		}
		if(dates.getMonthOfYear()==new DateTime().getMonthOfYear()){
			// 判断是否是本月
			for (int i = day; i >= 1; i--) {
				String days = String.valueOf(i);
				if(i<10){
					days = "0"+i;
				}
				String aDate = String.valueOf(year)+"-"+months+"-"+days;
				list.add(aDate);
			}
		}else{
			for (int i = 1; i <= day; i++) {
				String days = String.valueOf(i);
				if(i<10){
					days = "0"+i;
				}
				String aDate = String.valueOf(year)+"-"+months+"-"+days;
				list.add(aDate);
			}
		}
		return list;
	}

	public static String getCurDateDay(){
		Calendar aCalendar = Calendar.getInstance();
		int date = aCalendar.get(Calendar.DATE);
		if(date<10){
			return "0"+date;
		}else{
			return String.valueOf(date);
		}
	}

	/**
	 * 上月
	 * @return
	 */
	public static String getLastMonth(){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String lastMonth = sdf.format(c.getTime());
		return lastMonth;
	}
}
